/**
 * 功能描述 卡片
 */

define(function(require, exports, module) {
	var Form = require("form");
	var enmu = require("enums");
	var moment = require("moment");
	var applyFormEvents = require("../handler/applyFormEvents");
	var Dialog = require("dialog");
	var store = require("store");
	var activeUser = store.get("user");
	
	
	var instance = {
		initComponent : function(params, widget) {
			var perform = new Form({
				parentNode : ".J-perform",
				show : false,
				model : {
					id : "applyform",
					saveaction: function() {
                        var data = perform.getData();
                        applyFormEvents.saveAction(data, widget);
                    },
                    saveText: "保存",
                    cancelaction: function() {
                    	widget.show([".J-form", ".J-pergrid"]).hide(".J-perform");
						widget.get("subnav").show(["save", "return"]);
						widget.get("perform").reset();
                    },
                    cancelText: "取消",
					items : [{
                   	 	name : "pkSignUpRegister",
                   	 	type : "hidden",
					},{
                   	 	name : "version",
                   	 	type : "hidden",
                   	 	defaultValue : "0",
					},{
						name : "personalInfo.pkPersonalInfo",
						type : "hidden",
					},{
						name : "personalInfo.version",
						type : "hidden",
						defaultValue : "0",
					},{
						name : "member",
						type : "hidden"
					},{
						name : "customer",
						type : "hidden"
					},{
						name : "memberObj",
						label : "姓名",
						type:"autocomplete",
						url:"api/member/search",
						keyField:"pkMember",
						valueField:"personalInfo.idNumber",
						queryParamName : "s",
						params:function(str){
							return{
								searchProperties:"personalInfo.name", 
								fetchProperties:"pkMember,personalInfo.*"
							}
						},
						format:function(data){
							return data.personalInfo?data.personalInfo.name:"";
						},
						onItemSelect : function(data){
							perform.setValue("member", true);
							perform.setValue("customer", false);
							
							perform.setValue("personalInfo.pkPersonalInfo",data.personalInfo.pkPersonalInfo);
							perform.setValue("personalInfo.version",data.personalInfo.version);
							//身份证号
							perform.setValue("personalInfo.idNumber",data.personalInfo.idNumber);
							//性别
							perform.setValue("personalInfo.sex",data.personalInfo.sex.key);
							perform.setReadonly("personalInfo.sex",true);
							//联系方式
							if(data.personalInfo.mobilePhone){
								perform.setValue("personalInfo.mobilePhone",data.personalInfo.mobilePhone);
							};
						},
						validate : [ "required" ]
					},{
						name : "personalInfo.name",
						label : "姓名",
						type:"text",
						validate : [ "required" ]
					},{
						name : "idNumberSearch",
						label : "身份证号",
						type:"autocomplete",
						url:"api/customer/search",
						keyField:"personalInfo.idNumber",
						valueField:"personalInfo.idNumber",
						queryParamName : "s",
						params:function(str){
							return{
								searchProperties:"personalInfo.idNumber", 
								fetchProperties:"personalInfo.*"
							}
						},
						format:function(data){
							return data.personalInfo.idNumber + " - " + data.personalInfo.name;
						},
						onItemSelect : function(data){
							perform.setValue("member", false);
							perform.setValue("customer", true);
							
							perform.setValue("personalInfo.pkPersonalInfo",data.personalInfo.pkPersonalInfo);
							perform.setValue("personalInfo.version",data.personalInfo.version);
							//身份证号
							perform.setValue("personalInfo.name",data.personalInfo.name);
							perform.setValue("personalInfo.idNumber",data.personalInfo.idNumber);
							//性别
							perform.setValue("personalInfo.sex",data.personalInfo.sex.key);
							//联系方式
							if(data.personalInfo.mobilePhone){
								perform.setValue("personalInfo.mobilePhone",data.personalInfo.mobilePhone);
							};
						},
						onNoMatch: function(){
							perform.setValue("member", false);
							perform.setValue("customer", false);
						},
						mustMatch: false,
						validate : [ "required" ]
					},{
						name : "personalInfo.idNumber",
						label : "身份证号",
						type : "text",
						validate : [ "required" ]
					},{
						name : "personalInfo.sex",
						label : "性别",
						type : "radio",
						defaultValue : "Male",
						list : enmu["com.eling.elcms.basedoc.model.Sex"],
						validate : [ "required" ]
					},{
						name : "personalInfo.mobilePhone",
						type : "text",
						label : "手机号",
						validate : [ "required" ],
						exValidate: function(value) {
                            var reg = /^1(3|4|5|7|8)\d{9}$/;
                            if (!reg.test(value)) {
                                return "请输入正确的手机号！";
                            } else {
                                return true;
                            }
                        },
					},{
						name : "contactName",
						label : "紧急联系人",
						exValidate: function(value){
							if(value.length > 10){
								return "紧急联系人名字不能超过10个字符！";
							}else{
								return true;
							}
						},
					},{
						name : "contactPhone",
						label : "紧急联系人电话",
						exValidate: function(value){
							var reg = /^1(3|4|5|7|8)\d{9}$/;
							if(value!=""){
								if (!reg.test(value)) {
		                            return "请输入正确的手机号！";
		                        } else {
		                            return true;
		                        }
							}else{
								return true;
							}
	                        
						}
					},{
						name : "travelContractObj",
						label : "会员证号",
						type: "autocomplete",
                        url: "api/signupregister/querycertificates",
                        params:function(str){
	                       	return {
	                       		"pkServicePoint":activeUser.servicePoint.pkServicePoint,
	                       		"fetchProperties":"pkTravelContract,memberCertificates,version,member.personalInfo.name," +
	                       				"member.personalInfo.idNumber,productRule.product.name,productRule.product.pkProduct," +
	                       				"productRule.product.version"
	                       	};
                        },
                        queryParamName: "s",
                        //代表所选的选项的值是哪一个字段的值
                        keyField: "pkTravelContract",
                        //代表选项下拉框中显示的值
                        format: function(data) {
                            return data.member.personalInfo.idNumber+"-"+data.member.personalInfo.name+
                            		"-"+data.productRule.product.name+"-"+data.memberCertificates;
                        },
                        onItemSelect : function(data){
                        	perform.setValue("pkTravelContract",data.pkTravelContract);
						},
						validate : [ "required" ]
					},{
						name : "pkTravelContract",
						type : "hidden",
					}]
				}
			});
			widget.set("perform", perform);
		}
	};
	module.exports = instance;
});
